﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.Validation;
using Kingdee.BOS.Core;
using Kingdee.BOS.App.Data;
using Kingdee.BOS;
using Kingdee.BOS.Util;


namespace KD.RAISER.SS.OUTSTOCK.SK.TK
{
    [Description("销售出库单保存删除插件"), HotUpdate]
    public class OUTSTOCK_SAVE_DEL : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            //e.FieldKeys.Add("FINVOICENO");
        }

        public override void BeginOperationTransaction(BeginOperationTransactionArgs e)
        {
            base.BeginOperationTransaction(e);
            string FSQL, FBillNo;
            string FID, FSALEORDERNO;

            if (this.FormOperation.Operation.ToString() == "Delete") // 删除
            {
                foreach (DynamicObject entity in e.DataEntitys)//entity当前单 e.DataEntitys所有单子的数据包
                {
                    FBillNo = entity["BillNo"].ToString();

                    FSQL = "/*dialect*/ SELECT FID FROM T_SAL_OUTSTOCK where FBillNo = '" + FBillNo + "'";
                    FID = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);

                    OUTSTOCK_DEL_AMOUNT(FID);
                }
            }
        }

        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            string FSQL, FBillNo;
            string FID, FSALEORDERNO;

            base.AfterExecuteOperationTransaction(e);

            if (this.FormOperation.Operation.ToString() == "Save") //保存
            {
                foreach (DynamicObject entity in e.DataEntitys)//entity当前单 e.DataEntitys所有单子的数据包
                {
                    FBillNo = entity["BillNo"].ToString();

                    FSQL = "/*dialect*/ SELECT FID FROM T_SAL_OUTSTOCK where FBillNo = '" + FBillNo + "'";
                    FID = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);


                    OUTSTOCK_SAVE_AMOUNT(FID);
                }
            }

        }



        public void OUTSTOCK_DEL_AMOUNT(string FID)
        {
            string FSQL, FSOORDERNO, FSOENTRYID, FSEQ, FREALRECAMOUNTFOR, FREALREFUNDAMOUNTFOR, FEntryID, FOUTQty;
            decimal F_SK_AMOUNT;

            FSQL = "/*dialect*/ SELECT FSOORDERNO,FSOENTRYID  FROM T_SAL_OUTSTOCKENTRY_R WHERE FID IN (" + FID + ") AND FSOORDERNO <> ''";
            DynamicObjectCollection dtEntry = DBUtils.ExecuteDynamicObject(this.Context, FSQL.ToString());
            if (dtEntry != null && dtEntry.Count > 0)
            {
                for (int a = 0; a < dtEntry.Count; a++)
                {
                    FSOORDERNO = dtEntry[a]["FSOORDERNO"].ToString();
                    FSOENTRYID = dtEntry[a]["FSOENTRYID"].ToString();



                    FSQL = "/*dialect*/ SELECT FSEQ FROM T_SAL_ORDERENTRY WHERE FEntryID = " + FSOENTRYID + "";
                    FSEQ = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);


                    FSQL = "/*dialect*/ select isnull(sum(FREALRECAMOUNTFOR),0) from T_AR_RECEIVEBILLENTRY " +
                    "where FSALEORDERNO ='" + FSOORDERNO + "' and FMATERIALSEQ =" + FSEQ + " " +
                    "and FID  in (select FID  from T_AR_RECEIVEBILL where  FDOCUMENTSTATUS ='C' ) ";
                    FREALRECAMOUNTFOR = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);

                    FSQL = "/*dialect*/ select isnull(sum(FREALREFUNDAMOUNTFOR),0) from T_AR_REFUNDBILLENTRY " +
                           "where FSALEORDERNUMBER ='" + FSOORDERNO + "' and FMATERIALSEQ =" + FSEQ + " " +
                           "and FID  in (select FID  from T_AR_REFUNDBILL where  FDOCUMENTSTATUS ='C') ";
                    FREALREFUNDAMOUNTFOR = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);

                    F_SK_AMOUNT = decimal.Parse(FREALRECAMOUNTFOR) - (decimal.Parse(FREALREFUNDAMOUNTFOR));

                    FSQL = "select /*dialect*/ ISNULL(SUM(FREALQTY),0) from T_SAL_OUTSTOCKENTRY WHERE  FENTRYID in (select FENTRYID  from T_SAL_OUTSTOCKENTRY_R where FSOORDERNO ='" + FSOORDERNO + "'  and  FSOENTRYID = " + FSOENTRYID + ") AND FID <> "+FID+"";
                    FOUTQty = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);

                    if (decimal.Parse(FOUTQty) != 0)
                    {

                        FSQL = "/*dialect*/ update T_SAL_OUTSTOCKENTRY set  F_SK_AMOUNT =(" + F_SK_AMOUNT + ") / (" + FOUTQty + ") * FREALQTY   where FENTRYID in (select FENTRYID  from T_SAL_OUTSTOCKENTRY_R where FSOORDERNO ='" + FSOORDERNO + "'  and  FSOENTRYID = " + FSOENTRYID + ")";
                        DBUtils.Execute(this.Context, FSQL);

                    }
                }
            }
        }


        public void OUTSTOCK_SAVE_AMOUNT(string FID)
        {
            string FSQL, FSOORDERNO, FSOENTRYID, FSEQ, FREALRECAMOUNTFOR, FREALREFUNDAMOUNTFOR, FEntryID, FOUTQty;
            decimal F_SK_AMOUNT;

            FSQL = "/*dialect*/ SELECT FSOORDERNO,FSOENTRYID  FROM T_SAL_OUTSTOCKENTRY_R WHERE FID IN (" + FID + ") AND FSOORDERNO <> ''";
            DynamicObjectCollection dtEntry = DBUtils.ExecuteDynamicObject(this.Context, FSQL.ToString());
            if (dtEntry != null && dtEntry.Count > 0)
            {
                for (int a = 0; a < dtEntry.Count; a++)
                {
                    FSOORDERNO = dtEntry[a]["FSOORDERNO"].ToString();
                    FSOENTRYID = dtEntry[a]["FSOENTRYID"].ToString();



                    FSQL = "/*dialect*/ SELECT FSEQ FROM T_SAL_ORDERENTRY WHERE FEntryID = " + FSOENTRYID + "";
                    FSEQ = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);


                    FSQL = "/*dialect*/ select isnull(sum(FREALRECAMOUNTFOR),0) from T_AR_RECEIVEBILLENTRY " +
                    "where FSALEORDERNO ='" + FSOORDERNO + "' and FMATERIALSEQ =" + FSEQ + " " +
                    "and FID  in (select FID  from T_AR_RECEIVEBILL where  FDOCUMENTSTATUS ='C' ) ";
                    FREALRECAMOUNTFOR = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);

                    FSQL = "/*dialect*/ select isnull(sum(FREALREFUNDAMOUNTFOR),0) from T_AR_REFUNDBILLENTRY " +
                           "where FSALEORDERNUMBER ='" + FSOORDERNO + "' and FMATERIALSEQ =" + FSEQ + " " +
                           "and FID  in (select FID  from T_AR_REFUNDBILL where  FDOCUMENTSTATUS ='C') ";
                    FREALREFUNDAMOUNTFOR = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);

                    F_SK_AMOUNT = decimal.Parse(FREALRECAMOUNTFOR) - (decimal.Parse(FREALREFUNDAMOUNTFOR));

                    FSQL = "select /*dialect*/ ISNULL(SUM(FREALQTY),0) from T_SAL_OUTSTOCKENTRY WHERE  FENTRYID in (select FENTRYID  from T_SAL_OUTSTOCKENTRY_R where FSOORDERNO ='" + FSOORDERNO + "'  and  FSOENTRYID = " + FSOENTRYID + ")";
                    FOUTQty = DBUtils.ExecuteScalar<string>(this.Context, FSQL, string.Empty, null);

                    if (decimal.Parse(FOUTQty) != 0)
                    {
                        FSQL = "/*dialect*/ update T_SAL_OUTSTOCKENTRY set  F_SK_AMOUNT =(" + F_SK_AMOUNT + ") / (" + FOUTQty + ") * FREALQTY   where FENTRYID in (select FENTRYID  from T_SAL_OUTSTOCKENTRY_R where FSOORDERNO ='" + FSOORDERNO + "'  and  FSOENTRYID = " + FSOENTRYID + ")";
                        DBUtils.Execute(this.Context, FSQL);
                    }

                }
            }
        }
    }
}
